import Vue from 'vue'
import VueRouter from 'vue-router'
import Acount from '../views/Acount'
import Register from '../views/Acount/Register'
import Mycenter from '../views/Acount/Center'
import Movie from '../views/Movie/'
import CityList from '../components/CityList/'
import Cinema from '../views/Cinema'
import Preview from '../views/Preview'
import Search from '../views/Search'
import Hot from '../views/Movie/Hot'
import Fhot from '../views/Movie/Fhot'
import Movieshow from '../views/Movieshow'
import Shows from '../views/Shows'
import SelectSeat from '../components/SelectSeat'
// import _this from '../main-dev'
// import _this2 from '../main-prod'
import { Toast } from 'vant'
Vue.use(VueRouter)
// Vue.prototype.$Toast = Toast

const routes = [
  {
    path: '/',
    redirect: '/movie/hot',
    meta: {
      keepAlive: false
    }
  },
  {
    path: '/account/login',
    component: Acount,
    meta: {
      keepAlive: false
    }
  },
  {
    path: '/account/register',
    component: Register,
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/account/mycenter',
    component: Mycenter
  },
  {
    path: '/movie',
    component: Movie,
    children: [
      {
        path: 'hot',
        component: Hot
      }, {
        path: 'f-hot',
        component: Fhot
      }
    ]
  },
  {
    path: '/city-list',
    component: CityList
  },
  {
    path: '/cinema',
    component: Cinema
  },
  {
    path: '/preview/:id',
    component: Preview

  }, {
    path: '/search',
    component: Search
  },
  {
    path: `/shows/:id/:mid`,
    component: Shows
  },
  {
    path: '/seats',
    component: SelectSeat
  },
  {
    path: '/movie/:id',
    component: Movieshow
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // const toDepth = to.path.split('/').length
  // const fromDepth = from.path.split('/').length
  // if (toDepth < fromDepth) {
  //   console.log('后退。。。')
  //   from.meta.keepAlive = false
  //   to.meta.keepAlive = true
  // }
  if (to.path === '/seats') {
    if (localStorage.getItem('users')) {
      next()
    } else {
      Toast('请登录')
      setTimeout(() => {
        next('/account/login')
      }, 800)
    }
  } else {
    //  thi
    next()
  }
  if (to.path === '/account/mycenter') {
    if (localStorage.getItem('users')) {
      next()
    } else {
      next('/movie/hot')
    }
  }
})

export default router
